﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace SpiderServer
{
	public class JobManager
	{
		private static Dictionary<int, Job> s_jobs = new Dictionary<int, Job>();
		private static object s_syncRoot;

		static JobManager()
		{
			s_syncRoot = (s_jobs as ICollection).SyncRoot;
		}

		public static Job GetJob(int jobId)
		{
			Job job = null;
			s_jobs.TryGetValue(jobId, out job);
			return job;
		}

		public static void Start(int jobId)
		{
			lock (s_syncRoot)
			{
				Job job = null;
				if (!s_jobs.TryGetValue(jobId, out job))
				{
					job = new Job(jobId);
					s_jobs.Add(jobId, job);
				}
				job.Start();
			}
		}

		public static void Stop(int jobId)
		{
			lock (s_syncRoot)
			{
				Job job = null;
				if (s_jobs.TryGetValue(jobId, out job))
				{
					job.Stop();
					s_jobs.Remove(jobId);
				}
			}
		}
	}
}
